import Vue from 'vue'
import VueRouter from 'vue-router'
 
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter);

const routes = [
    // 主页路径
    {
        path: '/',
        name: 'Index',
        component: () => import('../views/index.vue'),
        redirect: '/content',
        children: [{
                path: '/content',
                name: 'Content',
                component: () => import("../views/content")
            },
            {
                path: '/lifesharing',
                name: 'lifesharing',
                meta: { requiresAuth: true },  // 需要认证的页面
                component: () => import("../views/lifesharing.vue")
            },
            {
                path: '/learningexchange',
                name: 'learningexchange',
                meta: { requiresAuth: true },  // 需要认证的页面
                component: () => import("../views/learningexchange.vue")
            },
            {
                path: '/suggestion',
                name: 'suggestion',
                meta: { requiresAuth: true },  // 需要认证的页面
                component: () => import("../views/suggestion.vue")
            },
            {
                path: '/personal',
                name: 'personal',
                component: () => import("../views/personal.vue"),
                meta: { requiresAuth: true },  // 需要认证的页面
            },
            {
                path: '/userlike',
                name: 'userlike',
                component: () => import("../views/userslike.vue")
            },
            {
                path: '/userfocu',
                name: 'userfocu',
                component: () => import("../views/userfocu.vue")
            },
            {
                path: '/myfans',
                name: 'myfans',
                component: () => import("../views/myfans.vue"),
                
            },
            {
                path: '/search',
                name: 'search',
                component: () => import("../views/search.vue"),
                meta: { requiresAuth: true },  // 需要认证的页面
                
            }
            
        ]
    },
    // 登录
    {
        path: '/login',
        name: 'login',
        component: () => import("../views/login.vue")
    },
    // 注册
    {
        path: '/register',
        name: 'register',
        component: () => import("../views/register.vue")
    },
    // 详情
    {
        path: '/detail',
        name: 'detail',
        component: () => import("../views/detail"),
        meta: { requiresAuth: true },  // 需要认证的页面
    },

    // 后台管理
    {
        path: '/manage',
        name: 'manage',
        component: () => import("../views/manage/index.vue"),
        meta: { requiresAuth: true },  // 需要认证的页面
        redirect: '/manage/maglifesharing', // 重定向到子路由
        children: [{
                path: 'maglifesharing', // 相对路径
                name: 'maglifesharing',
                component: () => import("../views/manage/lifesharing.vue")
            },
            {
                path: 'maglearningexchange', // 相对路径，去掉开头的 /
                name: 'maglearningexchange',
                component: () => import("../views/manage/learningexchange.vue")
            },
            {
                path: 'magcomment', // 相对路径，去掉开头的 /
                name: 'magcomment',
                component: () => import("../views/manage/comment.vue")
            },
            {
                path: 'maguser', // 相对路径，去掉开头的 /
                name: 'maguser',
                component: () => import("../views/manage/user.vue")
            },
            {
                path: 'magpostsection', // 相对路径，去掉开头的 /
                name: 'magpostsection',
                component: () => import("../views/manage/postsection.vue")
            },
            {
                path: 'magsuggestion', // 相对路径，去掉开头的 /
                name: 'magsuggestion',
                component: () => import("../views/manage/suggestion.vue")
            }
            
        ]
    }

];





// const router = new Vuerouter({
//     mode: 'hash',
//     routes,
   
// });
const createRouter = () => new VueRouter({
    mode: 'history',
    routes: routes,
    scrollBehavior(to, from, savedPosition) {
        return {
            x: 0,
            y: 0
        }
    }
})
const router = createRouter()

router.beforeEach((to, from, next) => {
    let user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    console.log('当前用户:', user);  // 打印 sessionStorage 中的用户信息
  
    if (to.matched.some(record => record.meta.requiresAuth)) {
      if (!user.id) {
        alert("未登录，跳转到登录页面")
        console.log('未登录，跳转到登录页面');
        next({ name: 'login' });  // 如果没有登录则跳转
      } else {
        console.log('已登录，继续访问');
        next();  // 如果已登录，则继续访问目标路由
      }
    } else {
      console.log('目标路由不需要认证，继续访问');
      next();  // 不需要认证的路由直接访问
    }
  });
  
  

  
export default router;